

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.*;
import com.yz.dao.LogDao;
import com.yz.dao.RegionalDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Service("regionalService")
public class RegionalServiceImpl extends ServiceImpl<RegionalDao,RegionalEntity> implements RegionalService {
    @Autowired
    RegionalRemian1Service regionalRemian1Service;
    @Autowired
    RegionalRemian2Service regionalRemian2Service;
    @Autowired
    DrawingService drawingService;
    @Autowired
    CameraService cameraService;
    @Autowired
    CheckService checkService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postMe(RegionalForm data,UserEntity user) {
        RegionalEntity t = data.getRegionalEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(t.getUpdateAt());
        check.setCreatorId(user.getUserId());
        check.setCreateUser(t.getCreateUser());
        check.setName(projectService.selectById(t.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[0]);
        check.setProjectId(t.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(1);
        long id=0;
        if(t.getId()==0){//插入数据的情况
            this.baseMapper.insert(t);//插入主表
            id = this.baseMapper.selectId();//获取id
            //加入数据审核
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[0]+id);
            checkService.insert(check);
        }else{//更新数据
            id = t.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[0]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[0]+id));
            //更新附表前先删除附表数据
            regionalRemian1Service.delete(new EntityWrapper<RegionalRemain1Entity>().eq("regional_id",id));
            regionalRemian2Service.delete(new EntityWrapper<RegionalRemain2Entity>().eq("regional_id",id));
            //更新主表
            this.baseMapper.update(t,new EntityWrapper<RegionalEntity>().eq("id",id));
        }
        //插入或者更新遗物
        List<RegionalRemain1Entity> remains1 =data.getRegionalRemain1Entities();
        List<RegionalRemain2Entity> remains2 =data.getRegionalRemain2Entities();
        for (int i = 0; i < remains1.size(); i++) {
            remains1.get(i).setRegionalId(id);
            regionalRemian1Service.insert(remains1.get(i));
        }

        for (int i = 0; i < remains2.size(); i++) {
            remains2.get(i).setRegionalId(id);
            regionalRemian2Service.insert(remains2.get(i));
        }
        //图片放入数据库
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("type_form",1).eq("data_id",id));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("type_form",1).eq("data_id",id));
        if(data.getDrawingNumber()!=null&&data.getDrawingNumber().size()>0){
            for (int i = 0; i <data.getDrawingNumber().size() ; i++) {
                DrawingEntity item1 = data.getDrawingNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(t.getCreateTime());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(t.getProjectId());
                item1.setTypeWhere(1);
                item1.setTypeForm(1);
                item1.setDataId(id);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[0]+id);
                drawingService.insert(data.getDrawingNumber().get(i));
            }
        }
        if(data.getCameraNumber()!=null&&data.getCameraNumber().size()>0){
            for (int i = 0; i <data.getCameraNumber().size() ; i++) {
                CameraEntity item1 = data.getCameraNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(t.getCreateTime());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(t.getProjectId());
                item1.setTypeWhere(1);
                item1.setTypeForm(1);
                item1.setDataId(id);
                item1.setFiledName("0");
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[0]+id);
                cameraService.insert(data.getCameraNumber().get(i));
            }
        }
        if(data.getSectionNumber()!=null&&data.getSectionNumber().size()>0){
            for (int i = 0; i <data.getSectionNumber().size() ; i++) {
                CameraEntity item1 = data.getSectionNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(t.getCreateTime());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(t.getProjectId());
                item1.setTypeWhere(1);
                item1.setTypeForm(1);
                item1.setDataId(id);
                item1.setFiledName("1");
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[0]+id);
                cameraService.insert(data.getSectionNumber().get(i));
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public RegionalForm getRegioanlDetail(long id) {
        RegionalEntity r = this.baseMapper.selectById(id);
        List<RegionalRemain1Entity> remain1=regionalRemian1Service.selectList(new EntityWrapper<RegionalRemain1Entity>().eq("regional_id",id));
        List<RegionalRemain2Entity> remain2=regionalRemian2Service.selectList(new EntityWrapper<RegionalRemain2Entity>().eq("regional_id",id));
        RegionalForm data = new RegionalForm();
        RegionalForm from = new RegionalForm();
        StringBuffer dd = new StringBuffer();
        StringBuffer cc1 = new StringBuffer();
        StringBuffer cc2 = new StringBuffer();
        List<DrawingEntity> dds =  drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("type_form",1).eq("data_id",id));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("type_form",1).eq("data_id",id).eq("filed_name","0"));
        List<CameraEntity> cc2s =cameraService.selectList(new EntityWrapper<CameraEntity>().eq("type_form",1).eq("data_id",id).eq("filed_name","1"));
        from.setDrawingNumber(dds);
        from.setCameraNumber(cc1s);
        from.setSectionNumber(cc2s);
       from.setRegionalEntity(r);
        from.setRegionalRemain1Entities(remain1);
        from.setRegionalRemain2Entities(remain2);
        return from;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteMe(long id) {
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("type_form",1).eq("data_id",id));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("type_form",1).eq("data_id",id));
        this.baseMapper.deleteById(id);
        checkService.deleteMe(5,1,id);
        regionalRemian1Service.delete(new EntityWrapper<RegionalRemain1Entity>().eq("regional_id",id));
        regionalRemian2Service.delete(new EntityWrapper<RegionalRemain2Entity>().eq("regional_id",id));
    }
    @Override
    public List<Map<String,Object>> selectReginal(Map map) {
        return this.baseMapper.selectReginal(map);
    }

    @Override
    public int selectCountP(Map map) {
        return this.baseMapper.selectCountP(map);
    }
}
